package de.tum.in.eist.poll.client;

import java.util.List;
import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.corechart.PieChart.PieOptions;
import com.google.gwt.visualization.client.visualizations.corechart.PieChart;

import de.tum.in.eist.poll.shared.Result;

public class ResultsView {
	static void paint(){
		if(Poll.student == null){
			History.newItem("LecturerLogin");
			return;
		}
		RootPanel.get("logo").addStyleName("logo-small");

		final VerticalPanel surveyContainer = new VerticalPanel();
		surveyContainer.addStyleName("surveyContainer");
		class DateHandler implements ClickHandler, KeyUpHandler {
			private Date date;
			public DateHandler(Date d){
				date = d;
			}
			public void onClick(ClickEvent event) {
				changeUI();
			}
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					changeUI();
				}
			}
			public void changeUI() {
				Poll.currentLecDate = date;
				surveyContainer.clear();
				final VerticalPanel lecPanel = new VerticalPanel();
				lecPanel.addStyleName("lecPanel");
				final DateTimeFormat format = DateTimeFormat.getFormat("EEEE, d MMM yyyy 'at' HH:mm");
				Poll.pollService.getPolls(new AsyncCallback<List<de.tum.in.eist.poll.shared.Poll>>(){
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(List<de.tum.in.eist.poll.shared.Poll> result) {
						for(de.tum.in.eist.poll.shared.Poll d : result){
								Button lec = new Button(format.format(d.getDate()));
								lec.setStyleName("lecDateButton");
								if(d.getDate() == Poll.currentLecDate)
									lec.addStyleName("active");
								DateHandler dh = new DateHandler(d.getDate());
								lec.addClickHandler(dh);
								lecPanel.add(lec);	
						}
						surveyContainer.add(lecPanel);
					}
				});
				Runnable onLoadCallback = new Runnable() {
					public void run() {				 
				        // Create a pie chart visualization.
						PieOptions options = PieOptions.create();
						options.setWidth(400);
						options.setHeight(240);
						options.set3D(true);
						options.setTitle("Satisfaction");
						
						DataTable data = DataTable.create();
					    data.addColumn(ColumnType.STRING, "Task");
					    data.addColumn(ColumnType.NUMBER, "Hours per Day");
					    data.addRows(2);
					    data.setValue(0, 0, "Work");
					    data.setValue(0, 1, 14);
					    data.setValue(1, 0, "Sleep");
					    data.setValue(1, 1, 10);
						PieChart pie = new PieChart(data, options);

				        surveyContainer.add(pie);
				      }
				    };

				    // Load the visualization api, passing the onLoadCallback to be called
				    // when loading is done.
				    VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);
			}
		}
		if(Poll.currentLecDate == null){
			Poll.pollService.getPolls(new AsyncCallback<List<de.tum.in.eist.poll.shared.Poll>>(){

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(List<de.tum.in.eist.poll.shared.Poll> result) {
					Poll.currentLecDate = result.get(0).getDate();
				}
			});
		}
		DateHandler dh = new DateHandler(Poll.currentLecDate);
		dh.changeUI();
		RootPanel.get("container").add(surveyContainer);
	}
}
